package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.ShopMapper;
import com.lxk.pojo.Shop;
import com.lxk.pojo.ShopExample;
import com.lxk.service.ShopService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/3/24 15:22
 * @description
 */
@Service
@Transactional
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper sm;
    @Autowired
    private AsMyMapper mm;


    public List<Shop> getShopList() {
        ShopExample example = new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        example.setOrderByClause("shop_id ASC");
        List<Shop> list = sm.selectByExample(example);
        return list;
    }

    public Integer getTotalPages(Integer num) {
        ShopExample example = new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        Integer count = (int)sm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Shop> selShopPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Shop> list = mm.selShopPage(start, num, sort, order);
        PageModel<Shop> model = new PageModel<Shop>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Shop selShopById(Integer shopId) {
        Shop shop = sm.selectByPrimaryKey(shopId);
        return shop;
    }

    public Integer modifyShopInfo(Shop shop) {
        int upd = sm.updateByPrimaryKeySelective(shop);
        return upd;
    }

    public Integer deleteShop(Integer shopId) {
        Shop shop = sm.selectByPrimaryKey(shopId);
        shop.setIsDelete(1);
        int upd = sm.updateByPrimaryKeySelective(shop);
        return upd;
    }

    public Integer addShop(Shop shop) {
        shop.setShopTime(new Date());
        shop.setModifyTime(new Date());
        int ins = sm.insertSelective(shop);
        if(ins==0) {
            throw new GlobalException(CodeMsg.ADD_SHOP_ERROR);
        }
        return 1;
    }


}
